package pro.softzhang.algo.lc300;

import pro.softzhang.algo.flag.knowledge.KDoublePointer;
import pro.softzhang.algo.flag.theme.TVowels;

@TVowels
public class LC345_ReverseVowelsOfAString {
    public static void main(String[] args) {
        System.out.println(new Solution().reverseVowels("abce"));
    }

    @KDoublePointer
    static
    class Solution {
        public String reverseVowels(String s) {
            char[] reversed = s.toCharArray();
            for(int lp = 0, rp = s.length() - 1; lp < rp; ) {
                char lc = reversed[lp];
                boolean isLcVowel = isVowel(lc);
                if (!isLcVowel) {
                    reversed[lp] = lc;
                    lp++;
                }

                char rc = reversed[rp];
                boolean isRcVowel = isVowel(rc);
                if(!isRcVowel) {
                    reversed[rp] = rc;
                    rp--;
                }

                if(isRcVowel && isLcVowel) {
                    reversed[lp++] = rc;
                    reversed[rp--] = lc;
                }
            }
            return new String(reversed);
        }

        private boolean isVowel(char ch) {
            return ch == 'a' || ch == 'A'
                    || ch == 'e' || ch == 'E'
                    || ch == 'i' || ch == 'I'
                    || ch == 'o' || ch == 'O'
                    || ch == 'u' || ch == 'U';
        }
    }
}